package com.example.forum.service.impl;

import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.dao.ArticleMapper;
import com.example.forum.dao.BoardMapper;
import com.example.forum.dao.UserMapper;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Article;
import com.example.forum.model.Board;
import com.example.forum.model.User;
import com.example.forum.service.IArticleService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ArticleServiceImpl implements IArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserServiceImpl userService;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private BoardServiceImpl boardService;

    @Transactional//打包成一个事务
    @Override
    public Integer insertSelective(Article article) {
        if(!StringUtils.hasLength(article.getContent()) ||
            !StringUtils.hasLength(article.getTitle()) ||
            article.getBoard_id() < 0) {
            log.warn("Article相关参数校验失败");
            throw new ApplicationException("Article相关参数校验失败");
        }
        article.setDelete_state(0);
        article.setLike_count(0);  //点赞数
        article.setReply_count(0); //回复数
        article.setVisit_count(0); //访问数
        article.setUpdate_time(new Date(System.currentTimeMillis()));
        article.setCreate_time(new Date(System.currentTimeMillis()));
        Integer ret1 = articleMapper.insertSelective(article);
        if(ret1 < 0 || ret1 == null) {
            log.warn("插入Article失败");
            throw new ApplicationException("插入Article失败");
        }
        Integer ret2 = userService.updateArticleCount(article);
        if(ret2 < 0 || ret2 == null) {
            log.error("更新用户文章数失败");
            throw new ApplicationException("更新用户文章数失败");
        }

        Integer ret3 = boardService.updateBoardArticleCount(article);
        if(ret3 < 0 || ret3 == null) {
            log.warn("更新board文章数失败");
            throw new ApplicationException("更新board文章数失败");
        }
        return ret1;
    }

    @Override
    public List<Article> selectAllByBoardId() {
        List<Article> lists = articleMapper.selectAllByBoardId();
        log.info("查找成功");
        return lists;
    }

    @Override
    public List<Article> selectByBoardId(Long boardId) {
        List<Article> lists = articleMapper.selectByBoardId(boardId);
        log.info("根据boardId查找成功");
        return lists;
    }

    @Override
    public Article selectArticle(Long articleId) {
        Article article = articleMapper.selectArticle(articleId);
        if(article == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }

        article.setVisit_count(article.getVisit_count()+1);
        Integer ret = articleMapper.updateByPrimaryKeySelective(article);
        if(ret == null) {
            log.warn("更新失败");
            throw new ApplicationException("更新失败");
        }
        String pathName = article.getUser().getAvatar_url();
        if(pathName != null) {
            Integer i = pathName.indexOf("image");
            if (i == null) {
                log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
                throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
            }
            article.getUser().setAvatar_url("./" + pathName.substring(i));
        }
        return article;
    }

    @Override
    public Article updateLikeCount(Long articleId) {
        Article article = articleMapper.selectByPrimaryKey(articleId);
        if(article == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        article.setLike_count(article.getLike_count() + 1);
        Integer ret = articleMapper.updateByPrimaryKeySelective(article);
        if(ret == null) {
            log.warn("更新失败");
            throw new ApplicationException("更新失败");
        }
        return article;
    }

    @Override
    public Integer deleteArticleById(Long articleId) {
        Article article = articleMapper.selectArticle(articleId);
        if(article == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }

        Board board = boardService.getBoardById(article.getBoard_id());
        if(board == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        board.setArticle_count(board.getArticle_count() - 1);

        User user = userMapper.selectByPrimaryKey(article.getUser_id());
        if(user == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        user.setArticle_count(user.getArticle_count() - 1);


        Integer ret = articleMapper.deleteArticle(articleId);
        Integer ret1 = boardService.updateBoard(article.getBoard_id());
        Integer ret2 = userMapper.updateByPrimaryKeySelective(user);
        if(ret == null || ret1 == null || ret2 == null) {
            log.warn("删除文章失败");
            throw new ApplicationException("删除文章失败");
        }
        log.info("删除文章成功");
        return ret;
    }

    @Override
    public Integer updateArticle(Article article) {
        Article article1 = articleMapper.selectByPrimaryKey(article.getId());
        if(article1 == null) {
            log.warn("文章不存在");
            throw new ApplicationException("文章不存在");
        }
        if(StringUtils.hasLength(article.getTitle())) {
            article1.setTitle(article.getTitle());
        }
        if(StringUtils.hasLength(article.getContent())) {
            article1.setContent(article.getContent());
        }
        Integer ret = articleMapper.updateByPrimaryKeySelective(article1);
        if(ret == null || ret <= 0) {
            log.warn("更新文章失败");
            throw new ApplicationException("更新文章失败");
        }
        log.info("更新文章成功");
        return ret;
    }

    @Override
    public List<Article> selectArticleByUserId(Long userId) {
        log.info("根据userId查找文章成功");
        return articleMapper.selectArticleByUserId(userId);
    }


    @Override
    public Article updateCountRise(Long articleId) {
        Article article = articleMapper.selectByPrimaryKey(articleId);
        if(article == null) {
            log.warn(ResultCode.FAILED_NOT_EXISTS.getMessage());
            throw new ApplicationException(ResultCode.FAILED_NOT_EXISTS.getMessage());
        }
        article.setLike_count(article.getLike_count() - 1);
        Integer ret = articleMapper.updateByPrimaryKeySelective(article);
        if(ret == null) {
            log.warn("更新失败");
            throw new ApplicationException("更新失败");
        }
        log.info("更新成功");
        return article;

    }
}
